/*
 * $URL: http://dnsprobe.googlecode.com/svn/trunk/dnsprobe/src/dnsprobe/data/lookup/LookupResult.java $
 * 
 * Copyright (c) 2009, Anonymous
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Anonymous nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY ANONYMOUS ''AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL ANONYMOUS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */

package dnsprobe.data.lookup;

import static dnsprobe.Constants.EMPTY_STRING;
import static dnsprobe.Constants.ADDRESS_SEPARATOR;

import java.net.InetAddress;
import java.util.HashSet;
import java.util.Iterator;


/**
 * Ein Lookup-Ergebnis
 * 
 * Haelt die Ergebnisse von einem Nameserver lookup auf einen Hostnamen
 * und stellt verschiedene Methoden fuer das bearbeiten bereit
 *
 * @author BerndBernhardson
 * @author anon004
 * @author snoop
 * @version $Id: LookupResult.java 164 2009-07-08 01:42:55Z bernhardson@gmx.net $
 *
 */
public class LookupResult
{
	/**
	 * die Adressliste
	 */
	private HashSet<InetAddress> addresses;
	
	
	/**
	 * Konstruktor
	 */
	public LookupResult()
	{
		this.addresses = new HashSet<InetAddress>();
	}

	/**
	 * Fuegt dem Ergebnis eine Adresse hinzu
	 * 
	 * @param address Ein InetAddress Objekt, darf nicht null sein
	 */
	public void addAddress(final InetAddress address)
	{
		if(address == null)
		{
			return;
		}
		
		this.addresses.add(address);
	}
	
	/**
	 * Liefert die Ergebnisliste
	 * 
	 * @return Die Ergebnisliste
	 */
	public HashSet<InetAddress> getAddresses()
	{
		return this.addresses;
	}
	
	/**
	 * Sagt ob Adressen vorliegen
	 * 
	 * @return true wenn Adressen vorliegen, ansonsten false
	 */
	public boolean hasAddresses()
	{
		return (this.addresses.size() > 0);
	}
	
	/**
	 * Liefert die Adressen als String, mehrere Adressen werden 
	 * mit kommas getrennt
	 * 
	 * @return Ein String mit allen Adressen
	 */
	@Override
	public String toString()
	{
		if(addresses.size() == 0)
		{
			return EMPTY_STRING;
		}
		
		if(addresses.size() == 1)
		{
			return addresses.iterator().next().getHostAddress();
		}
		
		final Iterator<InetAddress> iterator;
		final StringBuilder result;
		
		iterator = addresses.iterator();
		result   = new StringBuilder();
		
		while(iterator.hasNext())
		{
			final InetAddress element;
			
			element = iterator.next();
			
			result.append(element.getHostAddress());
			
			if(iterator.hasNext())
			{
				result.append(ADDRESS_SEPARATOR);
			}
		}

		return result.toString();
	}
	
	/**
	 * Vergleicht die Adressen mit einem anderen LookupResult
	 * 
	 * @param o ein LookupResult Objekt
	 * 
	 * @return true wenn beide Objekte die gleichen Adressen enthalten, 
	 *         false wenn o null ist oder nicht die gleichen Adressen enthaelt
	 */
	@Override
	public boolean equals(final Object o)
	{
		if(o == null || !(o instanceof LookupResult))
		{
			return false;
		}
		
		final LookupResult         result;
		final HashSet<InetAddress> list;
		
		result = (LookupResult)o;
		list   = result.getAddresses();
		
		// unterschiedliche anzahl
		if(addresses.size() != list.size())
		{
			return false;
		}
		
		// bei einem eintrag brauchen wir nicht drueber zu loopen
		if(addresses.size() == 1 && list.size() == 1)
		{
			return (addresses.iterator().next().equals(list.iterator().next()));
		}
		
		// gegenseitiges loopen
		for(final InetAddress inetAddress: addresses)
		{
			if(!list.contains(inetAddress))
			{
				return false;
			}
		}
		
		for(final InetAddress inetAddress: list)
		{
			if((!addresses.contains(inetAddress)))
			{
				return false;
			}
		}
		
		// zwei LookupResult objekte mit den gleichen adressen
		return true;
	}
}
